package com.mygame.arena.handler;


import com.mygame.arena.business.ArenaBusinessHandler;
import com.mygame.common.component.BaseRoute;
import com.mygame.common.component.RequestPacket;
import com.mygame.common.component.annotation.Handler;
import com.mygame.common.component.annotation.Route;
import com.mygame.common.msg.CommandEnum;
import com.mygame.common.msg.MessageType;
import com.mygame.common.msg.ServiceEnum;
import com.mygame.message.nats.NatsMessageContext;
import com.mygame.common.protobuf.ExternalMessage;
import com.mygame.common.protobuf.ExternalMessageUtil;
import com.mygame.common.protobuf.gameMsg.BuyArenaChallengeTimesRequest;
import com.mygame.common.protobuf.gameMsg.BuyArenaChallengeTimesResponse;
import com.mygame.common.protobuf.rpcMsg.ConsumeDiamondRequest;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.concurrent.Promise;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Route(serviceType = ServiceEnum.GAME_ARENA)
public class CommonHandler implements BaseRoute<RequestPacket> {
    private Logger logger = LoggerFactory.getLogger(ArenaBusinessHandler.class);

    @Override
    public void onReceive(RequestPacket packet) {
        System.out.println("CommonHandler");
    }


    //GATE_SERVER.CommonHandler.game.test
    @Handler(cmd = CommandEnum.C2S_TEST)
    public void test(RequestPacket packet) {
        System.out.println("CommonHandler");
    }


    @Handler(cmd = CommandEnum.C2S_BUY_ARENA_CHALLENGE_TIMES_REQ)
    public void buyChallengeTimes(BuyArenaChallengeTimesRequest request, NatsMessageContext ctx) {
        // 先通过rpc扣除钻石，扣除成功之后，再添加挑战次数

        Promise<ExternalMessage> rpcPromise = ctx.newRPCPromise();
        rpcPromise.addListener(new GenericFutureListener<Future<ExternalMessage>>() {
            @Override
            public void operationComplete(Future<ExternalMessage> future) throws Exception {
                ExternalMessage message = new ExternalMessage();
                BuyArenaChallengeTimesResponse response = new BuyArenaChallengeTimesResponse();
                if (future.isSuccess()) {
                    ExternalMessage rpcResponse = (ExternalMessage) future.get();
                    int errorCode = rpcResponse.getErrorCode();
                    if (errorCode == 0) {
//                        ctx.addChallengeTimes(10);// 假设添加10次竞技场挑战次
                        logger.debug("购买竞技挑战次数成功");
                        message = ExternalMessageUtil.build(CommandEnum.S2C_BUY_ARENA_CHALLENGE_TIMES_RSP, response);
                    } else {
                        message = ExternalMessageUtil.build(CommandEnum.S2C_BUY_ARENA_CHALLENGE_TIMES_RSP, errorCode);
                    }
                } else {
                    message = ExternalMessageUtil.build(CommandEnum.S2C_BUY_ARENA_CHALLENGE_TIMES_RSP, 1);
                }
                ctx.sendMessage(message);
            }
        });
        ConsumeDiamondRequest rpcRequest = new ConsumeDiamondRequest();
        rpcRequest.setConsumeCount(20);// 假设是20钻石
        ExternalMessage message = ExternalMessageUtil.build(CommandEnum.RPC_CONSUME_DIAMOND_REQ, rpcRequest);
        message.setMessageType(MessageType.RPC_REQUEST.id);
        ctx.sendRPCMessage(message, rpcPromise);
    }

//    @Rpc(cmd = CommandEnum.C2S_BUY_ARENA_CHALLENGE_TIMES_REQ)
//    public void buyChallengeTimes(BuyArenaChallengeTimesRequest request, NatsMessageContext ctx) {
//        // 先通过rpc扣除钻石，扣除成功之后，再添加挑战次数
//
//        Promise<ExternalMessage> rpcPromise = ctx.newRPCPromise();
//        rpcPromise.addListener(new GenericFutureListener<Future<ExternalMessage>>() {
//            @Override
//            public void operationComplete(Future<ExternalMessage> future) throws Exception {
//                ExternalMessage message = new ExternalMessage();
//                BuyArenaChallengeTimesResponse response = new BuyArenaChallengeTimesResponse();
//                if (future.isSuccess()) {
//                    ExternalMessage rpcResponse = (ExternalMessage) future.get();
//                    int errorCode = rpcResponse.getErrorCode();
//                    if (errorCode == 0) {
////                        ctx.addChallengeTimes(10);// 假设添加10次竞技场挑战次
//                        logger.debug("购买竞技挑战次数成功");
//                    } else {
//                        message.setErrorCode(errorCode);
//                    }
//                } else {
//                    message.setErrorCode(ArenaError.SERVER_ERROR.getErrorCode());
//                }
//                ctx.sendMessage(message);
//            }
//        });
//        ConsumeDiamondRequest rpcRequest = new ConsumeDiamondRequest();
//        rpcRequest.setConsumeCount(20);// 假设是20钻石
//        ExternalMessage message = ExternalMessageUtil.build(CommandEnum.RPC_CONSUME_DIAMOND_REQ, rpcRequest);
//        ctx.sendRPCMessage(message, rpcPromise);
//    }
}